<?php

class ImageProcessor
{
    public function Validate_Code()
    {
        $image = imagecreate(100, 30);
		$bg = imagecolorallocate($image, 255, 255, 255);
		$textcolor = imagecolorallocate($image,0,0,0);
	
		$alphabet='1Iqazxsw23WZXedcvfQASr45tgbCDnREhyFV67TGBuYUHjmNMKkliPOop098';
		$letter_num = rand(4,5);
	
		$width = round(100/$letter_num);
		$data = '';
		for($i=0;$i<$letter_num;$i++)
		{
			$x1=rand(0,99);
			$y1=rand(0,29);
			$x2=rand(0,99);
			$y2=rand(0,29);
			imageline($image,$x1,$y1,$x2,$y2,$textcolor);
			//imagestring($image,rand(2,5), $i*$width+rand(0,10), rand(0,10),$alphabet[rand(0,59)], $textcolor);
			$letter=$alphabet[rand(0,59)];
			$data.= $letter;
			$font = APP_PATH.'/Resources/login/ggbi.ttf';
                        $font_size = rand(15,20);
			imagettftext($image,$font_size,rand(-30,30),$i*$width+2,rand(18,25),$textcolor,$font,$letter);
		}
		header("content-type:image/png");
		imagepng($image,NULL,1);
		imagedestroy($image);
		$_SESSION['VerifyCode']=strtolower($data);
    }
    public function get_face_points($filename)
    {
        $fp = fsockopen(Yaf_Registry::get('param')->face_recognition_service, 6000, $errno, $errstr, 30);
        if (!$fp) {
                echo "$errstr ($errno)";
        }else {
                fwrite($fp,$filename);
                return fgets($fp,6000);
        }
    }
    public function upload_image($folder,$form)
    {
        $type = explode('/', $_FILES[$form]['type']);
        $type = strtolower($type[1]);
        if ($type=='jpeg' || $type=='pjpeg')
            $type = 'jpg';
        if ($type=='jpg' || $type=='png'|| $type =='gif')
        {
            $image_name = md5(microtime().rand(1,99999999)).'.'.$type;
            if (move_uploaded_file($_FILES[$form]['tmp_name'],'../'.$folder.'/'.$image_name))
                return $image_name;
            else 
                return false;
        }
        else 
            return false;
    }
	
    public function upload_multi_image($folder,$form)
    {
        $number = count($_FILES[$form]['type']);
        $names = array();
        for($i=0;$i<$number;$i++)
        {
            $type = explode('/', $_FILES[$form]['type'][$i]);
            $type = strtolower($type[1]);
            if ($type=='jpeg'|| $type=='pjpeg')
                $type = 'jpg';
            if ($type=='jpg' || $type=='png'|| $type =='gif')
            {
                $image_name = md5(microtime().rand(1,99999999)).'.'.$type;
                if (move_uploaded_file($_FILES[$form]['tmp_name'][$i],'../'.$folder.'/'.$image_name))
                    $names[] = $image_name;
            }
        }
        return $names;
    }
	
    public function thumbnail($folder,$image_name,$new_width,$new_height)
    {
        $ext = substr($image_name, -3);
        $fileurl = '../'.$folder;
        if ($ext=='png')
            $image = imagecreatefrompng($fileurl.'/'.$image_name);
        else if ($ext=='jpg')
            $image = imagecreatefromjpeg($fileurl.'/'.$image_name);
        else if ($ext=='gif')
            $image = imagecreatefromgif($fileurl.'/'.$image_name);
        else 
            return false;
        $width = imagesx($image);
        $height = imagesy($image);
        $thumb = imagecreatetruecolor($new_width,$new_height);
        imagecopyresampled($thumb,$image,0,0,0,0,$new_width,$new_height,$width,$height);
        imagejpeg($thumb,$fileurl.'/s_'.$image_name,50);
        imagedestroy($image);
        imagedestroy($thumb);
    }
}
?>
